package com.ptszyxx.popose.module.main.user;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.constants.BundleConstant;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.databinding.FragmentUserGuardBinding;
import com.ptszyxx.popose.module.main.user.adapter.UserGuardAdapter;
import com.ptszyxx.popose.module.main.user.vm.UserGuardVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.user.GuardEntity;
import com.ysg.http.data.entity.user.GuardResult;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YDialogUtil;
import com.ysg.utils.YImageUtil;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;
import com.ysg.widget.dialog.GiftDialog;
import com.ysg.widget.dialog.GuardDialog;
import com.ysg.widget.dialog.entity.GiftListResult;
import com.ysg.widget.dialog.entity.GiftNumResult;

import java.util.List;

public class UserGuardFragment extends BaseFragment<FragmentUserGuardBinding, UserGuardVM> {
    private UserGuardAdapter adapter;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_user_guard;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public UserGuardVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(UserGuardVM.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        Bundle bundle = getArguments();
        if (bundle != null){
            viewModel.userId = bundle.getString(BundleConstant.USER_ID);
            viewModel.txUserId = bundle.getString(BundleConstant.CHAT_USER_ID);
        }
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestGuardList();
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        adapter = new UserGuardAdapter();
        binding.refreshView.initAdapter(adapter);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onRefreshEvent.observe(this, new Observer<List<GuardEntity>>() {
            @Override
            public void onChanged(List<GuardEntity> list) {
                if (YCollectionUtil.isNotEmpty(list)){
                    setOtherUI(list);
                    setAdapterUI(list);
                }
            }
        });
        viewModel.uc.onGuardEvent.observe(this, o -> showDialogGuard((GuardResult)o));
        viewModel.baseUC.onGiftDialogEvent.observe(this, o -> showDialogGift());
    }

    private void setAdapterUI(List<GuardEntity> list) {
        if (list.size() > 1){
            for (int i = 0; i < list.size(); i++) {
                if (i == 0) {
                    list.remove(i);
                }
            }
        }
        binding.refreshView.setAdapterUI(adapter, list, true);
    }

    private void setOtherUI(List<GuardEntity> list) {
        GuardEntity entity = list.get(0);
        YImageUtil.show(binding.ivAvatarBg, entity.getPic());
        YImageUtil.show(binding.ivAvatar, entity.getPic());
        binding.tvLabel.setText(entity.getNick());
        String dayIntimacy = String.format(getResources().getString(R.string.user_guard_day_intimacy), entity.getDays(), entity.getIntimacy());
        binding.tvInfo.setText(dayIntimacy);
        if (YStringUtil.eq(YSPUtils.getInstance().getUserId(), entity.getUserId())) {
            binding.tvGuard.setText(getResources().getString(R.string.user_guard_intimacy_up));
        }
    }

    private void showDialogGuard(GuardResult entity) {
        YDialogUtil.getInstance().showGuard(getContext(), entity.getPic(), entity.getNick(), entity.getPayMoney())
                .setOnConfirmListener(new GuardDialog.OnConfirmListener() {
                    @Override
                    public void onConfirmClick() {
                        viewModel.onGiftClick();
                    }
                });
    }

    /**
     * 礼物
     */
    private void showDialogGift(){
        YDialogUtil.getInstance().showGift(getContext(), viewModel.giftListEntity, viewModel.giftNumResult)
                .setOnGiftListener(new GiftDialog.OnGiftListener() {
                    @Override
                    public void onRechargeClick() {
                        YActivityUtil.getInstance().jumpMineRecharge(viewModel);
                    }

                    @Override
                    public void onSendClick(GiftListResult.GiftBean giftBean, GiftNumResult.Data giftNumEntity) {
                        viewModel.requestSendGift(giftBean, giftNumEntity, viewModel.txUserId, null, (gifUrl)->
                            showDialogSvga(gifUrl)
                        );
                    }
                });
    }

    /**
     * 礼物SVGA
     */
    private void showDialogSvga(String url){
        YDialogUtil.getInstance().showSVGA(getContext(), url);
    }
}
